const tree = {
    id: 1,
    children: [
        {
            id: 2,
            children: [
                {
                    id: 3,
                    children: [
                        {
                            id: 4,
                            children: [],
                        },
                    ],
                },
                {
                    id: 5,
                    children: [],
                },
            ],
        },
        {
            id: 6,
            children: [],
        },
    ],
};

Object.prototype.dfsId = function(id) {
    if (!this) {
        return false
    }
    if (this.id === id) {
        return true
    }
    if (this.children?.length) {
        for (const node of this.children) {
            let res = node.dfsId(id)
            if (res) {
                return true
            }
        }
    }
    return false
}

console.log(tree.dfsId(7))
